﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebService_Newstore.DAO
{
    public class LgCmdDAO:BaseDAO
    {
        public int id;
        public int numeroCommande;
        public int numeroLigne;
        public int idArticle;
        public float QteCommandee;
        public float QteLivree;
        public int statut;
        public string code_uniteCmd;
        public float QteReference;                 // quantité de produit en unité de référence de l'article

        public LgCmdDAO() { }

        public LgCmdDAO(int id, int numCmd, int numLig, int idArt, float QteCmd, float QteLivree, int statut, string codeUnite, float QteRef)
        {
            this.id = id;
            this.numeroCommande = numCmd;
            this.numeroLigne = numLig;
            this.idArticle = idArt;
            this.QteCommandee = QteCmd;
            this.QteLivree = QteLivree;
            this.statut = statut;
            this.code_uniteCmd = codeUnite;
            this.QteReference = QteRef;
        }

        /// <summary>
        /// Renvoie la liste des lignes de commande pour une commande
        /// </summary>
        /// <param name="idCommande">int, l'identifiant de la commande</param>
        /// <returns>Liste des lignes de commandes</returns>
        public static List<LgCmdDAO> getLgCmdDAOByCmd(int idCommande)
        {
            List<LgCmdDAO> ListeLgCmd = new List<LgCmdDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from lg_commande in newStoreContext.lg_commande
                                where lg_commande.id_cmd == idCommande
                                select lg_commande;

                    if (query.LongCount() == 0)
                        throw new Exception("Aucune commande ne correspond !!");

                    foreach (var lg in query)
                    {
                        LgCmdDAO lgCmd = new LgCmdDAO();
                        lgCmd.id = lg.id_lgCmd;
                        lgCmd.numeroCommande = lg.id_cmd;
                        lgCmd.numeroLigne = (int)lg.num_lg;
                        lgCmd.idArticle = (int)lg.id_article;
                        lgCmd.QteCommandee = (int)lg.qte_cmd;
                        lgCmd.QteLivree = (int)lg.qte_liv;
                        lgCmd.statut = (int)lg.statut;
                        lgCmd.QteReference = 0;
                        lgCmd.code_uniteCmd = lg.VenUn;
                        //Il manque le id_unitéCmd à quoi ça correspond ?

                        ListeLgCmd.Add(lgCmd);
                    }
                    return ListeLgCmd;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    return ListeLgCmd;
                }
            }
        }

        //nombre de lignes non clôturées pour une commande donnée
        public static int CountRemainToDeliver(int idCommande)
        {
            List<LgCmdDAO> ListeLgCmd = new List<LgCmdDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from lg_commande in newStoreContext.lg_commande
                                where lg_commande.id_cmd == idCommande && lg_commande.statut < 3 
                                select lg_commande;

                    return query.Count();

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    return 0;
                }
            }
        }

        // chargement d'une ligne de commande par son id
        public static LgCmdDAO GetLgCmdById(int idLgCmd)
        {
            LgCmdDAO lgRetour = new LgCmdDAO();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    
                    var userQuery = from lg_commande in newStoreContext.lg_commande
                                    where lg_commande.id_lgCmd == idLgCmd
                                    select lg_commande;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un DAO vide pour faire remonter l'erreur au niveau du controller
                        lgRetour.id = 0;
                    }

                    foreach (var ligne in userQuery)
                    {
                        //On instancie la ligne de BP
                        lgRetour.id = ligne.id_lgCmd;
                        lgRetour.idArticle = (int)ligne.id_article;
                        lgRetour.numeroCommande = ligne.id_cmd;
                        lgRetour.numeroLigne = (int)ligne.num_lg;
                        lgRetour.QteCommandee = (float)ligne.qte_cmd;
                        lgRetour.QteLivree = (float)ligne.qte_liv;
                        lgRetour.QteReference = (float)ligne.RefQte;
                        lgRetour.statut = (int)ligne.statut;
                        lgRetour.code_uniteCmd = ligne.VenUn;
                    }

                    return lgRetour;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return lgRetour;
                }
            }
        }

        public static LgCmdDAO SaveLgCmd(LgCmdDAO lgcmd)
        {
             using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    lg_commande l = newStoreContext.lg_commande.FirstOrDefault(i => i.id_lgCmd == lgcmd.id);
                    if (l != null)
                    {
                        //Modification
                        l.id_cmd = lgcmd.numeroCommande;
                        l.num_lg = lgcmd.numeroLigne;
                        l.id_article = lgcmd.idArticle;
                        l.qte_cmd = lgcmd.QteCommandee;
                        l.qte_liv = lgcmd.QteLivree;
                        l.statut = lgcmd.statut;
                        l.VenUn = lgcmd.code_uniteCmd;
                        l.RefQte = lgcmd.QteReference;
                        newStoreContext.SaveChanges();
                        return lgcmd;
                    }
                    else
                    {
                        //Insertion
                        lg_commande lgtocreate = new lg_commande();
                        lgtocreate.id_cmd = lgcmd.numeroCommande;
                        lgtocreate.num_lg = lgcmd.numeroLigne;
                        lgtocreate.id_article = lgcmd.idArticle;
                        lgtocreate.qte_cmd = lgcmd.QteCommandee;
                        lgtocreate.qte_liv = lgcmd.QteLivree;
                        lgtocreate.statut = lgcmd.statut;
                        lgtocreate.VenUn = lgcmd.code_uniteCmd;
                        lgtocreate.RefQte = lgcmd.QteReference;
                        newStoreContext.lg_commande.AddObject(lgtocreate);
                        newStoreContext.SaveChanges();
                        lgcmd.id = lgtocreate.id_lgCmd;
                        return lgcmd;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                    return lgcmd;
                }
            }
        }
    }
}
